import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:r_scan/r_scan.dart';
import 'package:r_scan/src/r_scan_camera.dart'; // 显式导入相机相关功能

void main() {
  // 初始化测试绑定
  TestWidgetsFlutterBinding.ensureInitialized();
  const MethodChannel rScanChannel = MethodChannel('com.rhyme_lph/r_scan');
  const MethodChannel cameraChannel = MethodChannel('com.rhyme_lph/r_scan_camera/method');
  late RScanCameraController cameraController;
  late RScanCameraDescription mockCameraDescription;

  setUp(() {
    // 设置RScan通道的模拟方法调用处理器
    rScanChannel.setMockMethodCallHandler((MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'scanImagePath':
          final String path = methodCall.arguments['path'];
          return path.isNotEmpty ? {'message': 'Mock scan result for $path', 'type': 11} : null;
        case 'scanImageUrl':
          final String url = methodCall.arguments['url'];
          return url.isNotEmpty ? {'message': 'Mock scan result for $url', 'type': 11} : null;
        case 'scanImageMemory':
          final Uint8List uint8list = methodCall.arguments['uint8list'];
          return uint8list.isNotEmpty ? {'message': 'Mock scan result for memory data', 'type': 11} : null;
        default:
          return null;
      }
    });

    // 设置相机通道的模拟方法调用处理器
    cameraChannel.setMockMethodCallHandler((MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'availableCameras':
          return [
            {'name': 'front', 'lensFacing': 'front'},
            {'name': 'back', 'lensFacing': 'back'},
          ];
        case 'initialize':
          return {'textureId': 1, 'previewWidth': 1280, 'previewHeight': 720};
        case 'startScan':
          return null;
        case 'stopScan':
          return null;
        case 'setFlashMode':
          return true;
        case 'getFlashMode':
          return false;
        case 'setAutoFlashMode':
          return true;
        case 'dispose':
          return null;
        default:
          return null;
      }
    });

    // 创建模拟相机描述
    mockCameraDescription = RScanCameraDescription(
      name: 'back',
      lensDirection: RScanCameraLensDirection.back,
    );

    // 创建相机控制器
    cameraController = RScanCameraController(
      mockCameraDescription,
      RScanCameraResolutionPreset.medium,
    );
  });

  tearDown(() {
    rScanChannel.setMockMethodCallHandler(null);
    cameraChannel.setMockMethodCallHandler(null);
  });

  group('RScan静态方法测试', () {
    test('scanImagePath should return RScanResult when path is valid', () async {
      final result = await RScan.scanImagePath('test/path.jpg');
      expect(result.message, equals('Mock scan result for test/path.jpg'));
      expect(result.type, equals(RScanBarType.qr_code));
    });

    test('scanImagePath should return empty RScanResult when path is empty', () async {
      final result = await RScan.scanImagePath('');
      expect(result.message, isNull);
    });

    test('scanImageUrl should return RScanResult when url is valid', () async {
      final result = await RScan.scanImageUrl('https://test.com/image.jpg');
      expect(result.message, equals('Mock scan result for https://test.com/image.jpg'));
      expect(result.type, equals(RScanBarType.qr_code));
    });

    test('scanImageMemory should return RScanResult when data is provided', () async {
      final result = await RScan.scanImageMemory(Uint8List.fromList([1, 2, 3]));
      expect(result.message, equals('Mock scan result for memory data'));
      expect(result.type, equals(RScanBarType.qr_code));
    });
  });

  group('RScanCameraController测试', () {
    test('availableRScanCameras should return list of cameras', () async {
      final cameras = await availableRScanCameras();
      expect(cameras, isNotEmpty);
      expect(cameras.length, equals(2));
      expect(cameras[0].name, equals('front'));
      expect(cameras[1].name, equals('back'));
    });

    test('camera controller should initialize successfully', () async {
      await cameraController.initialize();
      expect(cameraController.value.isInitialized, isTrue);
      expect(cameraController.value.previewSize, isNotNull);
    });

    test('camera control methods should work correctly', () async {
      await cameraController.initialize();

      await cameraController.startScan();
      expect(await cameraController.setFlashMode(true), isTrue);
      expect(await cameraController.getFlashMode(), isFalse); // Mock returns false
      expect(await cameraController.setAutoFlashMode(true), isTrue);
      await cameraController.stopScan();
      await cameraController.dispose();
    });
  });
}
